// SetServerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "vfw.h"
#include "RMSServer.h"
#include "RMSServerDlg.h"
#include "SetServerDlg.h"

#include "../Capture/CapLib.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

///#define GET_SERVER_DLG    ((CRMSServerDlg*)AfxGetMainWnd())
#define GET_BASE_SERVER   (GET_SERVER_DLG->BaseServer)


/////////////////////////////////////////////////////////////////////////////
// CSetServerDlg dialog


CSetServerDlg::CSetServerDlg(RMSHostEnt *pHost, CWnd* pParent /*=NULL*/)
	: CDialog(CSetServerDlg::IDD, pParent),
		m_pHost(pHost)
{
	//{{AFX_DATA_INIT(CSetServerDlg)
	m_nPort = 0;
	m_nCycle = 0;
	m_rb320 = -1;
	m_rb640 = -1;
	//}}AFX_DATA_INIT
}


void CSetServerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSetServerDlg)
	DDX_Control(pDX, IDC_CYCLE, m_editCycle);
	DDX_Control(pDX, IDC_IP, m_cmbxIP);
	DDX_Text(pDX, IDC_PORT, m_nPort);
	DDV_MinMaxUInt(pDX, m_nPort, 0, 10000);
	DDX_Text(pDX, IDC_CYCLE, m_nCycle);
	DDV_MinMaxInt(pDX, m_nCycle, 1, 30);
	DDX_Radio(pDX, IDC_RADIO1, m_rb320);
	DDX_Radio(pDX, IDC_RADIO2, m_rb640);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSetServerDlg, CDialog)
	//{{AFX_MSG_MAP(CSetServerDlg)
	ON_BN_CLICKED(IDC_SETSLEEPIMAGE, OnBrowse)
	ON_BN_CLICKED(IDC_BUTTON1, OnClear)
	ON_BN_CLICKED(IDC_RADIO1, On320x240)
	ON_BN_CLICKED(IDC_RADIO2, On640x480)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSetServerDlg message handlers
extern bool bDisableCallback;
void CSetServerDlg::OnOK() 
{

	if(UpdateData(TRUE))
	{
		int n = m_cmbxIP.GetCurSel();
		if(n == -1)
		{
			m_cmbxIP.SetFocus();
			return;
		}
		m_cmbxIP.GetLBText(n, strIP);

#ifdef _CYCLIC_BUFFER_
		LONG lnRes;
		HKEY hKey;
		DWORD dwDisp;
		DWORD value;

		GET_BASE_SERVER.SetRecordCycle(m_nCycle);
		GET_BASE_SERVER.SignalDeleteOldFiles();

		lnRes = RegOpenKeyEx(HKEY_CURRENT_USER,"Software\\Streambox Everywhere\\Settings",0,KEY_ALL_ACCESS,&hKey);
		if(ERROR_SUCCESS != lnRes)
		{
			lnRes = RegCreateKeyEx(HKEY_CURRENT_USER,"Software\\Streambox Everywhere\\Settings",0,NULL,
				REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&hKey,&dwDisp);
			if(ERROR_SUCCESS != lnRes)
			{
				RegCloseKey(hKey);
				return;
			}
		}

		value = (DWORD)(m_nCycle);
		lnRes = RegSetValueEx(hKey,"Cycle",NULL,REG_DWORD,(BYTE*)&(value),sizeof(DWORD));
		RegCloseKey(hKey);
#endif
		CDialog::OnOK();
	}
}

BOOL CSetServerDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();

	if(!m_pHost)
	{
		return FALSE;
	}

#ifdef _CYCLIC_BUFFER_
	m_editCycle.EnableWindow(TRUE);

	m_nCycle = GET_BASE_SERVER.GetRecordCycle();
#else
	m_nCycle = 0;
	m_editCycle.EnableWindow(FALSE);
#endif

	/*

	hostent* hostinfo;
	char* szServer = new char[50];
	CString strIP;
	int i = 0;

	gethostname(szServer, 50);
	hostinfo = gethostbyname(szServer);

	while(hostinfo->h_addr_list[i])
	{
		strIP.Format("%d.%d.%d.%d",		(BYTE)hostinfo->h_addr_list[i][0], 
										(BYTE)hostinfo->h_addr_list[i][1], 
										(BYTE)hostinfo->h_addr_list[i][2], 
										(BYTE)hostinfo->h_addr_list[i][3]);

		m_cmbxIP.AddString((LPCTSTR) strIP);
		++i;
	}
	*/
	IPArr arr;
	m_pHost->GetIPArr(arr);
	IPArr::const_iterator it = arr.begin();
	while(it != arr.end())
	{
		String str = (*it).second;
		m_cmbxIP.AddString( str.c_str());
		++it;
	}

	m_cmbxIP.SetCurSel(m_cmbxIP.FindString(-1,GET_SERVER_DLG->m_strIP));

	m_nPort = GET_SERVER_DLG->m_nPort;

//	delete[] szServer;


//---- Disable 640x480 for OEM versions
#ifdef _OEM_
// 
	if (GetDlgItem(IDC_RESOLUTION)) {
		GetDlgItem(IDC_RESOLUTION)->EnableWindow(FALSE);
		GetDlgItem(IDC_RESOLUTION)->SetWindowText("Resolution is available in Streambox Presenter only");
	}
	GetDlgItem(IDC_RADIO1)->EnableWindow(FALSE);
	GetDlgItem(IDC_RADIO2)->EnableWindow(FALSE);
#endif


	UpdateData(FALSE);	

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CSetServerDlg::OnBrowse() 
{
	// TODO: Add your control notification handler code here
	CFileDialog dlg(1,0,0,OFN_HIDEREADONLY,"BMP Image|*.BMP|");
    if (dlg.DoModal()==IDOK) {
		LoadAndStoreBMP((char*)(LPCSTR)dlg.GetPathName());
	}
}

LRESULT PASCAL FrameCallbackProc(HWND hWnd, LPVIDEOHDR lpVHdr);

void CSetServerDlg::LoadAndStoreBMP(char *name)
{
    FILE *f;
    f = fopen(name,"rb");
    if (!f) return;

    int size=filelength(fileno(f));

    if (pSleepBitmapDataCompressed)
        free(pSleepBitmapDataCompressed);


    pSleepBitmapData = (BYTE*)malloc(size);

    fread(pSleepBitmapData,size,1,f);
    fclose(f);

    BYTE *tmp;
    int newsize;
    LPBITMAPINFOHEADER lp=(LPBITMAPINFOHEADER)(pSleepBitmapData+sizeof(BITMAPFILEHEADER));
    

	if ((lp->biWidth!=320) || (lp->biHeight!=240) ||(lp->biBitCount!=24)) {
		MessageBox("320x240 24-bit RGB bitmap is required","Cannot set custom image",MB_OK);
		goto error_exit;
	}

    CompressStillImage(((BYTE*)lp)+(lp->biSize),(void**)&tmp,&newsize);
    pSleepBitmapDataCompressed = (BYTE*)malloc(newsize);
    memcpy(pSleepBitmapDataCompressed,tmp,newsize);
    sizeSleepCompressed = newsize;

    char filename[1000];
    char *fn2;
    GetModuleFileName(0,filename,1000);

    fn2=strrchr(filename,'\\');
    fn2[0]=0;

    strcat(filename,"\\default.sleep");

    f = fopen(filename,"wb");
    fwrite(tmp,newsize,1,f);
    fclose(f);

error_exit:

    if (pSleepBitmapData)
        free(pSleepBitmapData);
}

void CSetServerDlg::OnClear() 
{
	// TODO: Add your control notification handler code here
    if (pSleepBitmapDataCompressed)
        free(pSleepBitmapDataCompressed);
	pSleepBitmapDataCompressed=0;

    char filename[1000];
    char *fn2;
    GetModuleFileName(0,filename,1000);

    fn2=strrchr(filename,'\\');
    fn2[0]=0;

    strcat(filename,"\\default.sleep");
    remove(filename);	
}

void CSetServerDlg::On320x240() 
{
	// TODO: Add your control notification handler code here
    m_rb640=-1;	
    m_rb320=0;
    UpdateData(false);
}

void CSetServerDlg::On640x480() 
{
	// TODO: Add your control notification handler code here
    m_rb640=0;	
    m_rb320=-1;	
    UpdateData(false);
}
